博客迁徙到GitHub Pages

将博客从Heroku迁徙到GitHub Pages,还是喜欢Markdown,网上搜了一下,发现有一个基于Python的静态网页生成器Pelican,很强大的样子,我原先的博客是基于octopress的,估计格式稍微修改一下就可直接上Pelican了。鉴于本人之前的博客有不少,一篇一篇修改不现实,直接用Python修改吧:

2014.10.24: 将博客从heroku的octopress迁徙到github pages:这段代码实现将目录下面所有的.markdown结尾的文件都替换为.md结尾
dirpath = "/Users/Heros/dev/git/pelican/content/articles/"
for f in os.listdir(dirpath):
    oldfpath = dirpath + f
    print oldfpath
    newfpath = dirpath + f.replace(".markdown", ".md")
    print newfpath
    os.rename(oldfpath, newfpath)
2014.10.24: 将博客从heroku的octopress迁徙到github pages:这段代码实现了将目录下所有文件里面第2行后面插入一行,内容为“Author: mainframer”
dirpath = "/Users/Heros/dev/git/pelican/content/articles/"
for f in os.listdir(dirpath):
    fpath = dirpath + f
    fp = file(fpath, 'r')
    lines = []
    for line in fp:
        lines.append(line)
    fp.close()
    #print type(lines) lines现在是list了
    lines.insert(1, "Author: mainframer\n")
    s = ''.join(lines)
    fp = file(fpath, 'w')
    fp.write(s); fp.close()
2014.10.24:将博客从heroku的octopress迁徙到github pages:这段代码实现了将目录下所有文件里面所有内容为'---'的行都删除掉
dirpath = "/Users/Heros/dev/git/pelican/content/articles/"
for f in os.listdir(dirpath):
    fpath = dirpath + f
    fp = file(fpath, 'r')
    lines = []
    for line in fp:
        if line != "---\n":
            lines.append(line)
    fp.close()
    #print type(lines) lines现在是list了
    s = ''.join(lines)
    fp = file(fpath, 'w')
    fp.write(s); fp.close()
2014.10.24:将博客从heroku的octopress迁徙到github pages:这段代码实现了将目录下所有文件里面所有内容为'date:'都替换成'Date:'
dirpath = "/Users/Heros/dev/git/pelican/content/articles/"
for f in os.listdir(dirpath):
    fpath = dirpath + f
    fp = file(fpath, 'r')
    lines = []
    for line in fp:
        if 'date:' in line:
            line = line.replace('date:', 'Date:')
        lines.append(line)
    fp.close()
    #print type(lines) lines现在是list了
    s = ''.join(lines)
    fp = file(fpath, 'w')
    fp.write(s); fp.close()

Comments !